package com.stone.activemqcase.config;/*

 */

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * 主题生产者
 * @ClassName Producer_topic
 * @Author Final
 * @Project DataStructures
 */
public class Producer_topic {

    public static final String ACTIVEMQ_URL="tcp://127.0.0.1:61616";
    public static final String TOPIC_NAME="topic-stone";
    public static void main(String[] args) throws JMSException {
        //1、创建连接工厂
        ActiveMQConnectionFactory factory=new ActiveMQConnectionFactory(ACTIVEMQ_URL);

        //2、通过连接工厂获得连接connection并访问
        Connection connection = factory.createConnection();


        //3、创建会话session
        //第一个参数叫事物第二个叫签收
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        //4、创建目的地(具体是主题或者队列)Destination destination可以是主题或者队列
        Topic topic = session.createTopic(TOPIC_NAME);

        //5、创建生产者
        MessageProducer producer = session.createProducer(topic);
        producer.setDeliveryMode(DeliveryMode.PERSISTENT);
        connection.start();

        //6、通过生产者生产三条消息到队列中
        for (int i=1;i<4;i++){
            //7、创建消息
            TextMessage textMessage = session.createTextMessage("msg--------------------->" + i);
            //8、生产者发送给mq
            producer.send(textMessage);
        }

        //9、关闭资源
        producer.close();
        session.close();
        connection.close();

        System.out.println("----------------------------------->结束");
    }

}
